package com.example.demo53.methods.net;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.example.demo53.R;
import com.example.demo53.util.been.Light_item;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import static com.example.demo53.ui.Group.GroupFragment.light_items;

public class receive_message extends Thread{
    public  String ID;
    public  String KIND;
    private DatagramSocket socket ;
    private boolean isFinish = false;
    private int port1=8080;
    public static boolean pw=false;
    public Handler mHandler;
    public  String IID1;
    public  String KIND1;
private boolean test;
    @Override
    public void run() {

        try {
            //1.获取 datagramSocket 实例,并监听某个端口
            socket = new DatagramSocket(port1);
            while (!isFinish) {  //添加判断防止阻塞
                //2.创建一个 udp 的数据包
                byte[] buf = new byte[512];
                DatagramPacket packet = new DatagramPacket(buf, buf.length);

                //3.开始阻塞获取udp数据包
                try {
                    socket.receive(packet);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String recIp = packet.getAddress().toString().substring(1);//ip获取
                //substring(start , end (不取)) ; (1):截除第二位之前的内容 ； (1 ,str.indexOf("?"))指定位置开始截取
                buf=packet.getData();
                int nLen = packet.getLength();
                String rec3 = new String(buf,0,nLen,"GBK");
                if(rec3.substring(0,4).equals("0000")){
                    
                // 处理收到的消息
                    pw = true;
                    KIND=rec3.substring(4,5);
                    ID = rec3.substring(5);
                    //----------------去重---------------------
                    if (!dd(ID)){
                    Light_item item1 =new Light_item(KIND,ID);
                        light_items.add(item1);
                    switch (KIND){
                        case "1":
                            item1.setImg(R.mipmap.uni_light1);
                            break;
                        case "2":
                            item1.setImg(R.mipmap.uni_light1);
                            break;
                        case "3":
                            item1.setImg(R.mipmap.uni_light1);
                            break;
                    }
                       }
                 
                    //contains()包含元素则返回true,但是无法检测出类的重复
//                    if(!light_items.contains(item1)){
//                               light_items.add(item1);
//
//                       test= light_items.contains(item1);
//                    }

//-----------------非静态变量无法在线程实例化后提取(活动类------------------------
//                    pw = true;
//                    KIND = "123";
//                    //KIND=rec3.substring(4,5);
//                    IID = rec3.substring(5);
                }

                if (packet != null) {

                    /**
                     * 给客户端发送消息
                     */
                    byte[] receiveMsg = ("send successfully!"+pw).getBytes();
                    DatagramPacket receivePacket = new DatagramPacket(receiveMsg,
                            receiveMsg.length,
                            InetAddress.getByName(recIp), //目标地址
                            port1);      //广播端口

                    socket.send(receivePacket);

                }

            }
            //关闭资源
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
            //忽略错误  ？？
        }finally {
            exit();
        }


    }
    public void exit(){
        if (socket != null){
            socket.close();
            socket = null;
        }
        isFinish = true;
    }

    public  boolean getpw(){
        return pw;
    }
    public  boolean Rpw(){
        pw = false;
        return pw;
    }


    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getKIND() {
        return KIND;
    }

    public void setKIND(String KIND) {
        this.KIND = KIND;
    }
    private boolean dd(String a){
        boolean c = false;
        for (Light_item i :light_items){
            if(i.getId().equals(a)){
c=true;            }
        }
return c;
    }
}